<?php

/**
* AdminMenuTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminMenuTest extends WebTestCase
{

    private $testCodeName,$testSlideName,$testDesc,$testCodeId,$testSlideId,$url;
    private $username = '';
    private $password = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $this->testCodeName = md5(uniqid(rand(), true));
        $this->testSlideName = md5(uniqid(rand(), true));
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $this->code = substr(md5(uniqid(rand(), true)),0,6);
        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 

        $dbh->exec("INSERT INTO guide ( code, owner, publish ) VALUES ('".$this->code."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?cmd=AdminCodeList');
        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

    }

    public function testCodeList()
    {
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->assertText('>Lista kodów');
    }

    public function testCodeListRemoveCode()
    {
        // >Lista tras>Usuń trasę
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->assertText('>Lista kodów');
        $this->assertLinkById('AdminRemoveCode'.$this->testCodeId);
        $this->clickLinkById('AdminRemoveCode'.$this->testCodeId);
        $this->assertPattern("/.*$this->testCodeName.*/","URL: ".$this->url.'?cmd=AdminRemoveCode&id='.$this->testCodeId." Nie usuwaj kodu ".$this->testCodeName);
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Usuń kod');
        $this->clickSubmit("Tak");
        $this->assertText('>Lista kodów');
    }


    public function testCodeListDoNotRemoveCode()
    {
        // >Lista tras>Usuń trasę
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->assertText('>Lista kodów');

        $this->assertLinkById('AdminRemoveCode'.$this->testCodeId);
        $this->clickLinkById('AdminRemoveCode'.$this->testCodeId);
  
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Usuń kod');
        $this->clickSubmit('Nie');
        $this->assertText('>Lista kodów');
    }


    public function testCodeListAddCode()
    {
        //>Lista tras >Dodaj trasę 
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->clickLink('[Dodaj kod]');
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Dodaj kod');
        $this->assertTrue($this->setFieldByName('name', $this->testCodeName )); 
        $this->assertTrue($this->setFieldByName('desc', $this->testCodeName )); 
        $this->click("Zapisz");
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Dodaj kod');

    }

    public function testCodeListEditCode()
    {

        //>Lista tras > Edycja kodu
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->clickLink($this->testCodeName);
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Edycja kodu');
        $this->click("Zapisz");
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Edycja kodu');

    }



    public function testAuth()
    {
        //Login
        $this->get($this->url.'?cmd=AuthForm');
        $this->assertText('>Login');
        $this->assertLink('Nowy użytkownik',$this->url.'?cmd=AuthRegister');
        //Logut form
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->assertText('>');
        $this->assertLink('Logout',$this->url.'?cmd=AdminCodeList&logout=1');
        $this->assertText('>Lista kodów');
        //Register
        $this->get($this->url.'?cmd=AuthRegister');
        $this->assertText('>');
        $this->assertLink('Login',$this->url.'?cmd=AuthForm');
        $this->assertText('>Dodaj użytkownika');
    }


    public function testCodeListSendCode()
    {
        //>Lista tras >Pokaż trasę 
        $this->setConnectionTimeout(30);
        $this->get($this->url.'?cmd=AdminCodeList');
        $this->clickLink('[Wyślij kody]');
        $this->assertText('>');
        $this->assertLink('Lista kodów',$this->url.'?cmd=AdminCodeList');
        $this->assertText('>Wyślij kody');

    }

    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM guide WHERE owner='".$this->username."'" );
        $dbh->exec("DELETE FROM guide_name WHERE name='".$this->testCodeName."'" );
        $dbh->exec("DELETE FROM guide_desc WHERE desc='".$this->testCodeName."'" );
        $dbh = null;

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminCodeList&logout=1');
        
    }
 

}
?>
